
/**
 * Keeps track of DVD transactions. 
 * Note: Not all transactions are in total effect immediately.
 */
public class DVDTransaction {
	public static final String STATUS_RENTAL = "R";
	public static final String STATUS_SALE = "S";
	public static final String STATUS_NA = " ";
	
	private int code;
	private String dvdName;
	private int numberOfCopies; 	
	private String rentalStatus;//? questionably useful
	private int priceInCents;
	
	/**
	 * Retrieves DVD transaction code
	 * @return the code
	 */
	public int getCode() {
		return code;
	}

	/**
	 * Retrieves DVD name
	 * @return the name
	 * */
	public String getDVDName() {
		return dvdName;
	}
	
	/**
	 * Utility function to create a DVD transaction object, from a parsed line
	 * @param line the DVD transaction line
	 * @return the DVDTransaction object created from the parsed line
	 */
	static DVDTransaction createTransactionFromLine(String line){
		//System.out.println("Creating from line " + line);
		
		final int TRANS_CODE_SIZE = 2;
		final int TRANS_NAME_SIZE = 25;
		final int TRANS_NUMBER_SIZE = 3;
		final int TRANS_STATUS_SIZE = 1;
		final int TRANS_PRICE_SIZE = 6;
		
		int start_index = 0;
		final int EXPECTED_SIZE = TRANS_CODE_SIZE + 1 + TRANS_NAME_SIZE + 1 + TRANS_NUMBER_SIZE + 1 +
				TRANS_STATUS_SIZE + 1 + TRANS_PRICE_SIZE;
		if (line.length () != EXPECTED_SIZE){
			System.out.println("Expected " + EXPECTED_SIZE + " character lines in Merged Transaction File.");
			return null;
		}
		//Read in transaction code (2 characters)
		String code = line.substring(start_index, start_index + TRANS_CODE_SIZE);
		start_index += TRANS_CODE_SIZE + 1;
		
		//Read in transaction name (25 characters)
		String name = line.substring(start_index, start_index + TRANS_NAME_SIZE);
		name = name.trim();//Remove unnecessary whitespace
		start_index += TRANS_NAME_SIZE + 1;
		
		//Read in transaction amount (XXX)
		String number = line.substring(start_index, start_index + TRANS_NUMBER_SIZE);
		start_index += TRANS_NUMBER_SIZE + 1;
		
		//Read in transaction status (R or S)
		String status = line.substring(start_index, start_index + TRANS_STATUS_SIZE);
		start_index += TRANS_STATUS_SIZE + 1;
		
		//Read in price (XXX.XX)
		String price = line.substring(start_index, start_index + TRANS_PRICE_SIZE);
		int priceInCents = (int)Double.parseDouble(price)*100;
//		System.out.println("Created transaction " + code +", " + name +", "+ number +", " + status);
		//Return the parsed DVDTransaction object
		return new DVDTransaction(Integer.parseInt(code), name, Integer.parseInt(number), status, priceInCents);
	}
	
	
	/**
	 * Create a new DVD transaction
	 * @param transCode transaction code
	 * @param name name of DVD in transaction
	 * @param copies copies of DVD 
	 * @param rental status of rental
	 * @param priceCents price in cents of the transaction
	 */
	public DVDTransaction(int transCode, String name, int copies, String rental, int priceCents){
		code = transCode;
		dvdName = name;
		numberOfCopies = copies;
		rentalStatus = rental;
		priceInCents = priceCents;
	}
	
	/**
	 * Get the number of copies in the transaction
	 * @return the number of copies for the transaction
	 */
	public int getNumberOfCopies(){
		return numberOfCopies;
	}
	
	/**
	 * Get the rental status of the transaction
	 * @return the rental status of the transaction
	 */
	public String getRentalStatus() {
		return rentalStatus;
	}

	/**
	 * Get the price in cents of the transaction
	 * @return the price in cents for the transaction
	 */
	public int getPriceInCents() {
		return priceInCents;
	}
}